# Title     : TODO
# Objective : TODO
# Created by: Administrator
# Created on: 2019/7/24

library(ggrepel)
library(optparse)
library(magrittr)
library(ggpubr)
library(tidyverse)
library(FSA)
library(RJSONIO)
library(car)

createWhenNoExist <- function(f) {
  !dir.exists(f) && dir.create(f)
}


option_list <- list(
  make_option("--i", default = "AllMet_Raw.txt", type = "character", help = "metabolite data file"),
  make_option("--g", default = "group.txt", type = "character", help = "sample group file"),
  make_option("--config", default = "config.json", type = "character", help = "config file")
)
opt <- parse_args(OptionParser(option_list = option_list))

args <- commandArgs(trailingOnly = F)
scriptPath <- dirname(sub("--file=", "", args[grep("--file", args)]))
source(str_c(scriptPath, "/base.R"))

configJson <- fromJSON(opt$config)

diffMethod <- configJson$method
fcMethod <- configJson$fcMethod
isInter <- configJson$isInter %>%
  as.logical()
execTrend <- configJson$execTrend %>%
  as.logical()
mcInfoList <- configJson$mcInfos
groupSort <- configJson$groups
trendMethod <- configJson$trendMethod
isPaired <- configJson$isPaired %>%
  as.logical()

options(digits = 3)

sampleInfo <- read_tsv(opt$g) %>%
  rename(SampleID = Sample) %>%
  select(c("SampleID", "ClassNote")) %>%
  mutate(ClassNote = factor(ClassNote, levels = unique(ClassNote)))

groups <- unique(sampleInfo$ClassNote)

data <- read_tsv(opt$i) %>%
  rename(Metabolite = 1) %>%
  gather("SampleID", "Value", -Metabolite) %>%
  inner_join(sampleInfo, by = c("SampleID")) %>%
  filter(ClassNote %in% groups)

sampleIds <- sampleInfo$SampleID


parent <- paste0("./")

getPList <- function(name) {
  pData <- data %>% filter(Metabolite == name)
  maxY <- max(pData$Value)
  minY <- min(pData$Value)
  yHeight <- (maxY - minY)
  pValueDf <- pValueData %>% filter(Metabolite == name)
  comparisons <- groups %>%
    as.character() %>%
    combn(2) %>%
    asplit(2)

  dfMethod <- pValueDf$test.method
  method <- if (dfMethod == "anova") {
    "t.test"
  }else if (dfMethod == "kruskal.test") {
    "wilcox.test"
  }else dfMethod

  testTb <- comparisons %>%
    map2_dfr(1:length(comparisons), function(groups, i) {
      group1 <- groups[1]
      group2 <- groups[2]
      data1 <- pData %>%
        filter(ClassNote == group1) %>%
        .$Value
      data2 <- pData %>%
        filter(ClassNote == group2) %>%
        .$Value

      p <- if (method == "t.test") {
        tryCatch({
          test <- t.test(data1, data2, paired = isPaired)
          test$p.value
        }, error = function(e) {
          1
        })
      }else {
        test <- wilcox.test(data1, data2, paired = isPaired)
        test$p.value
      }
      if (is.na(p)) {
        p <- 1
      }
      y.position <- maxY + yHeight * (i) * 0.1
      p.signif <- if (p < 0.0001) "***" else if (p < 0.01) "**" else if (p < 0.05) "*" else "ns"
      tibble(group1 = group1, group2 = group2, p = p, p.signif = p.signif, y.position = y.position, method = method)
    }) %>%
    filter(p.signif != "ns")

  outTestTb <- testTb %>%
    mutate(Metabolite = name)
  list(testTb = outTestTb)
}

fileName <- paste0(parent, "/AllMet_Test.csv")

pValueData <- read.csv(fileName, header = T, stringsAsFactors = F, comment.char = "") %>%
  arrange(P)
names <- pValueData$Metabolite

list <- names %>%
  map(getPList)

testTb <- list %>%
  map_dfr(function(l) {
    l$testTb
  })

userTestTb <- testTb %>%
  select(-c("y.position")) %>%
  select("Metabolite", everything())

write_csv(userTestTb, "comparisons.csv")
write_csv(testTb, "testTb_comparisons.csv")
